package com.my.lock.v20;

import java.util.concurrent.atomic.AtomicBoolean;

public class Counter {
    private int count;
    private AtomicBoolean semaphore = new AtomicBoolean(false);

    public void increment() {
        for (; ; ) {
            if (this.semaphore.compareAndSet(false, true)) {
                this.count += 1;
                this.semaphore.set(false);
                break;
            }
        }
    }

    @Override
    public String toString() {
        return "Counter{" +
                "count=" + count +
                ", semaphore=" + semaphore +
                '}';
    }
}
